#include<iostream>

using namespace std;

const int N = 1e6 + 10;
int ne[N], h;

int main()
{
	int a;
	cin >> a;
	ne[h] = 1;
	ne[1] = 0;
	while (a--)
	{
		int i, x, y;
		cin >> i;
		if (i == 1)
		{
			cin >> x >> y;
			ne[y] = ne[x];
			ne[x] = y;
		}
		else if (i == 2)
		{
			cin >> x;
			cout << ne[x] << endl;;
		}
		else if (i == 3)
		{
			cin >> x;
			ne[x] = ne[ne[x]];
		}
	}

	return 0;
}
